package com.infinitus.nginxInfo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infinitus.nginxInfo.service.ConfigInfoService;

@Service
public class ConfigInfoServiceImpl implements ConfigInfoService {

	@Autowired
	private EntityManagerFactory entityManagerFactory;

	@Override
	public Map<String, String> getTopicTokens() {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		List<Object[]> res = entityManager.createNativeQuery("SELECT topic, token from clean_topic_token ").getResultList();
		Map<String, String> map = new HashMap<String, String>();
		for(Object[] arr: res){
			if(arr.length>1 && arr[0]!=null && arr[1]!=null){
				map.put(((String)arr[0]).trim(), ((String)arr[1]).trim());
			}
		}
		entityManager.clear();
		entityManager.close();
		return map;
	}
	
}
